package guda.house.web.action.bill;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.BillElecBiz;
import guda.house.biz.BillWaterBiz;
import guda.house.biz.OwnerBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.form.bill.BillElecEditForm;
import guda.house.biz.form.bill.BillElecForm;
import guda.house.biz.form.bill.BillWaterEditForm;
import guda.house.biz.form.bill.BillWaterForm;
import guda.house.biz.query.BillWaterQuery;
import guda.house.biz.query.RoleQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dao.BillWaterDAO;
import guda.house.dal.dataobject.*;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 15/11/15.
 */
@Controller
@RequestMapping(value = "bill/water")
public class BillWaterAction {

    @Autowired
    private BillWaterBiz billWaterBiz;
    @Autowired
    private OwnerBiz ownerBiz;
    @Autowired
    private BillWaterDAO billWaterDAO;

    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        BillWaterQuery billWaterQuery = new BillWaterQuery();
        billWaterQuery.setPageNo(pageId);
        billWaterQuery.setPageSize(pageSize);
        modelMap.put("query", billWaterQuery);
        BizResult bizResult = billWaterBiz.list(billWaterQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "bill/water/list.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }
    }

    @RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, BillWaterEditForm billWaterEditForm,
                       BindingResult result, Map<String, Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = billWaterBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            BillWaterDO billWaterDO = (BillWaterDO) bizResult.data.get("billWaterDO");
            billWaterEditForm.initForm(billWaterDO);

            BizResult bizResult1 = ownerBiz.queryForEdit(billWaterDO.getSuiteId());
            modelMap.putAll(bizResult1.data);

            SuiteDO suiteDO = (SuiteDO) bizResult1.data.get("suiteDO");
            UnitDO unitDO = (UnitDO) bizResult1.data.get("unitDO");
            BuildingDO buildingDO = (BuildingDO) bizResult1.data.get("buildingDO");
            billWaterEditForm.setVillageId(buildingDO.getVillageId());
            billWaterEditForm.setBuildingId(buildingDO.getBuildingId());
            billWaterEditForm.setUnitId(unitDO.getUnitId());
            billWaterEditForm.setSuiteId(suiteDO.getSuiteId());

            return "bill/water/edit.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }


    @RequestMapping(value = "/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, BillWaterForm billWaterForm,
                         BindingResult result) {
        return "bill/water/create.vm";
    }

    @RequestMapping(value = "/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap, @Valid BillWaterForm billWaterForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            BizResult bizResult = ownerBiz.queryForCreateReset(billWaterForm.getVillageId(), billWaterForm.getBuildingId(), billWaterForm.getUnitId());
            modelMap.putAll(bizResult.data);
            return "bill/water/create.vm";
        }
        if (billWaterForm.getRecordCurrent() - billWaterForm.getRecordLast() < 0) {
            result.rejectValue("recordCurrent", "recordCurrentError", "本月读数应该大于上月读数");
            BizResult bizResult = ownerBiz.queryForCreateReset(billWaterForm.getVillageId(), billWaterForm.getBuildingId(), billWaterForm.getUnitId());
            modelMap.putAll(bizResult.data);
            return "bill/water/create.vm";
        }

        BizResult bizResult = billWaterBiz.create(billWaterForm);
        if (bizResult.success) {
            return "redirect:/bill/water/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, @Valid BillWaterEditForm billWaterEditForm,
                           BindingResult result, Map<String, Object> model) {
        if (result.hasErrors()) {
            return "bill/water/edit.vm";
        }
        BillWaterDO billWaterDO = billWaterEditForm.toDO();
        BizResult bizResult = billWaterBiz.update(billWaterEditForm);
        if (bizResult.success) {
            return "redirect:/bill/water/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = billWaterBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }


    @RequestMapping(value = "/getLatest.htm")
    public void getLatest(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long suiteId = RequestUtil.getLong(request, "suiteId");
        BillWaterDO billWaterDO = billWaterDAO.queryLatest(suiteId);

        ResponseHelper.ajaxReturn(billWaterDO, response);
        return;


    }
}
